<?php
$start = time();
set_include_path ( $_SERVER ['DOCUMENT_ROOT'] );
include_once 'admin/includes/functions/admin-functions.php';
include_once 'admin/includes/functions/dashboard-functions.php';
global $DBobject, $SMARTY, $CONFIG;

// $termssql = "SELECT * FROM tbl_termcategory WHERE termcategory_deleted IS NULL";
// if($termres = $DBobject->wrappedSql($termssql)){
//   $SMARTY->assign("termgroups",$termres);
// }
$termssql = "SELECT DISTINCT(termcategory_id),termcategory_name,DATE_FORMAT(term_start_date,'%Y') AS year FROM tbl_term LEFT JOIN tbl_termcategory ON term_termcategory_id = termcategory_id WHERE term_deleted IS NULL AND term_published = 1 ORDER BY term_start_date DESC";
if($termres = $DBobject->wrappedSql($termssql)){
  $SMARTY->assign("termgroups",$termres);
}

$general = array();

// Load current term category ID
//$tsql = "SELECT term_object_id, term_termcategory_id,DATE_FORMAT(term_start_date,'%Y') AS term_start_date FROM tbl_term WHERE term_deleted IS NULL AND term_published = 1 AND term_start_date <= NOW() AND term_end_date >= NOW() ORDER BY term_start_date DESC LIMIT 1";
$tsql = "SELECT term_object_id, term_termcategory_id,DATE_FORMAT(term_start_date,'%Y') AS year FROM tbl_term WHERE term_deleted IS NULL AND term_published = 1 AND term_start_date <= NOW() ORDER BY term_start_date DESC LIMIT 1";
$res = $DBobject->wrappedSql($tsql);
foreach($termres as $tr){
  if($tr['termcategory_id'] == $res[0]['term_termcategory_id']){
    $SMARTY->assign("currentterm",$tr['termcategory_name']);
  }
} 
$current_term = $res[0]['term_termcategory_id'];
$current_year = $res[0]['year'];

if(!empty($_REQUEST['year'])){
	$current_year = $_REQUEST['year'];
}
if(!empty($_REQUEST['term'])){
  $current_term = $_REQUEST['term'];
}

$SMARTY->assign('sel_year',$current_year);
$SMARTY->assign('sel_term_id',$current_term);

if(!empty($current_term) && !empty($current_year)){
 	$enrol = enrolments(array("S","T", "MV"),null,null,null,$current_term,$current_year);
	$general[] = array("label"=>"Enrolments","value"=>number_format($enrol));
	
	$enrol = enrolments(array("S", "MV"),null,null,'approved',$current_term,$current_year);
	$general[] = array("label"=>"Paid Enrolments","value"=>number_format($enrol)); 
	
	$enrol = revenue(null,null,null,'approved',$current_term,$current_year);
	setlocale(LC_ALL,array('en_AU.utf8', 'en_AU'));
	$general[] = array("label"=>"Revenue","value"=>money_format('%.2n',$enrol));
	
 	$redata = reenrolments(null,null,null,$current_term,$current_year);
	$repercent = number_format(($redata['reenrolled']/$redata['previous'])*100,2);
	$general[] = array("label"=>"Re-enrolments(%)","value"=>$repercent."%");
	
	$trials = trials(null,null,null,$current_term,$current_year, null, true);
	if(!empty($trials)){
	  $trialconverts = trialconversions(null,null,null,$current_term,$current_year);
	  $general[] = array("label"=>"Trial Conversion (%)","value"=>number_format(($trialconverts/$trials)*100,2)."%");
	}else{
	  $general[] = array("label"=>"Trial Conversion (%)","value"=>number_format(0,2)."%");
	}
	
	$warnings = warnings();
	$general[] = array("label"=>"Warnings","value"=>number_format($warnings));
	
	$locations = locations();
	$general[] = array("label"=>"Locations","value"=>number_format($locations));
	
	
	//This section holds the Enrolment Status chart data
	$approved = enrolments(array("S", "MV"),null,null,'approved',$current_term,$current_year);
	$trial = enrolments(array("T"),null,null,'approved',$current_term,$current_year);
	$pending = enrolments(array("S", "MV"),null,null,'pending',$current_term,$current_year);
	$enrolment = array(array("label"=>"Paid","value"=>$approved),array("label"=>"Trial","value"=>$trial),array("label"=>"Pending","value"=>$pending));
	
	// This section shows Budget breakdown
	$currentyearenrolments = enrolmentbystate(array("S", "MV"),null,null,'approved',null,$current_year);
	$previousyearenrolments = enrolmentbystate(array("S", "MV"),null,null,'approved',null,$current_year-1);
	$budget = budget($current_year);
	$budgetlist = array();
	foreach($budget as $b){
	  $budgetlist[$b['term_region']]["budget"]=$b['budget'];
	}
	foreach($currentyearenrolments as $b){
	  $budgetlist[$b['term_region']]["current"]=$b['enrolment'];
	}
	foreach($previousyearenrolments as $b){
	  $budgetlist[$b['term_region']]["previous"]=$b['enrolment'];
	}
	$SMARTY->assign("budgetchartdata",$budgetlist); 
	
	// This section shows daily enrolments
	
	$daily = dailyEnrolments();
	
	// This section shows enrolments by state/territory
	$tsql = "SELECT term_termcategory_id FROM tbl_term WHERE term_deleted IS NULL AND term_published = 1 AND term_start_date <= NOW() ORDER BY term_start_date DESC LIMIT 1";
	//$tsql = "SELECT term_termcategory_id FROM tbl_term WHERE term_deleted IS NULL AND term_published = 1 AND term_start_date <= NOW() AND term_end_date >= NOW() ORDER BY term_start_date DESC LIMIT 1";
	$res = $DBobject->wrappedSql($tsql);
	
	$enrolmentsStateTerm= array();
	$year = $current_year;
	$term = intval($res[0]['term_termcategory_id']);
	$termD = enrolmentbystate(array("S", "MV"),null,null,'approved',$term,$year);
	$sumD = 0;$termDStates = array();
	foreach($termD as $t){
	  $termDStates[$t['term_region']] = $t;
	  $sumD = $sumD + $t['enrolment'];
	}
	
	
	$key1 = "Term {$term}";
	
	
	$term = $term-1;
	if($term <= 0){
	  $term = 4;$year = $year-1;
	}
	
	$termC = enrolmentbystate(array("S", "MV"),null,null,'approved',$term,$year);
	$sumC = 0;$termCStates = array();
	foreach($termC as $t){
	  $termCStates[$t['term_region']] = $t;
	  $sumC = $sumC + $t['enrolment'];
	}
	$key2 = "Term {$term}";
	
	
	$term = $term-1;
	if($term <= 0){
	  $term = 4;$year = $year-1;
	}
	
	$termB = enrolmentbystate(array("S", "MV"),null,null,'approved',$term,$year);
	$sumB = 0;$termBStates = array();
	foreach($termB as $t){
	  $termBStates[$t['term_region']] = $t;
	  $sumB = $sumB + $t['enrolment'];
	}
	$key3 = "Term {$term}";
	
	
	$term = $term-1;
	if($term <= 0){
	  $term = 4;$year = $year-1;
	}
	
	$termA = enrolmentbystate(array("S", "MV"),null,null,'approved',$term,$year);
	$sumA = 0;$termAStates = array();
	foreach($termA as $t){
	  $termAStates[$t['term_region']] = $t;
	  $sumA = $sumA + $t['enrolment'];
	}
	$key4 = "Term {$term}";
	
	$term = $term-1;
	if($term <= 0){
	  $term = 4;$year = $year-1;
	}
	

	
	$state = "SELECT DISTINCT(term_region) AS term_region FROM tbl_term WHERE term_deleted IS NULL";
	$resstate = $DBobject->wrappedSql($state);
	foreach($resstate as $rs){
	  if(!array_key_exists($rs['term_region'], $termDStates)){
	    $termDStates[$rs['term_region']] = array("term_region"=>$rs['term_region'],"enrolment"=>0);
	  }
	  if(!array_key_exists($rs['term_region'], $termCStates)){
	    $termCStates[$rs['term_region']] = array("term_region"=>$rs['term_region'],"enrolment"=>0);
	  }
	  if(!array_key_exists($rs['term_region'], $termBStates)){
	    $termBStates[$rs['term_region']] = array("term_region"=>$rs['term_region'],"enrolment"=>0);
	  }
	  if(!array_key_exists($rs['term_region'], $termAStates)){
	    $termAStates[$rs['term_region']] = array("term_region"=>$rs['term_region'],"enrolment"=>0);
	  }
	}
	
	ksort($termDStates);
	ksort($termCStates);
	ksort($termBStates);
	ksort($termAStates);
	
	$termDStates['Total'] = array('enrolment'=>$sumD,"term_region"=>"Total");
	$termCStates['Total'] = array('enrolment'=>$sumC,"term_region"=>"Total");
	$termBStates['Total'] = array('enrolment'=>$sumB,"term_region"=>"Total");
	$termAStates['Total'] = array('enrolment'=>$sumA,"term_region"=>"Total");
	
	
	$enrolmentsStateTerm["{$key1}"] = $termDStates;
	$enrolmentsStateTerm["{$key2}"] = $termCStates;
	$enrolmentsStateTerm["{$key3}"] = $termBStates;
	$enrolmentsStateTerm["{$key4}"] = $termAStates;
	$enrolmentsStateTerm =array_reverse($enrolmentsStateTerm,true);
	
	
	$colours = array("#48ac29","#009bdb","#f55d0f","#ffae00","#0bd0cd","#d6165e","#008081","#3a6475","#9b53d2","#ffdf06");
	$i=0;
	foreach($enrolment as &$val){
	  $val['colourid'] = $i;
	  $val['colour'] = $colours[$i];
	  $i++;
	}
	/* $i=0;
	foreach($enrolmentsStateTerm as &$val){
	  $val['colour'] = $colours[$i];
	  $i++;
	} */
	$i=0;
	foreach($daily as &$val){
	  $val['colourid'] = $i;
	  $val['colour'] = $colours[$i];
	  $i++;
	}
	$i=0;
	foreach($budgetlist as &$val){
	  $val['colourid'] = $i;
	  $val['colour'] = $colours[$i];
	  $i++;
	}
	
	
	$SMARTY->assign("enrolmentstatetermchartdata",$enrolmentsStateTerm);
	$SMARTY->assign("enrolmentstatetermfirstkey",key($enrolmentsStateTerm));
	$SMARTY->assign("enrolmentpiechartdata",$enrolment);
	$SMARTY->assign("dailychartdata",$daily);
	$SMARTY->assign("budgetchartdata",$budgetlist);
	
// 	$general[] = array("label"=>"Processeing Time","value"=>number_format(time()-$start));
	$SMARTY->assign("generaldata",$general);
}